package cn.yunlang.zhangZhongFei.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.yunlang.zhangZhongFei.entity.UserCollect;
import cn.yunlang.zhangZhongFei.service.UserCollectService;
import cn.yunlang.zhangZhongFei.util.JwtUtil;
import cn.yunlang.zhangZhongFei.vo.request.TokenRequest;
import cn.yunlang.zhangZhongFei.vo.request.UserCollectPageVo;
import cn.yunlang.zhangZhongFei.vo.request.UserCollectVo;
import cn.yunlang.zhangZhongFei.vo.response.R;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/UserCollect")
@Api(tags = "用户收藏")
public class UserCollectController {

  @Resource private UserCollectService userCollectService;

  @ApiOperation("分页查")
  @GetMapping("/page")
  public R<IPage<UserCollectPageVo>> page(
      @RequestParam Integer pageNo,
      @RequestParam Integer pageSize,
      @RequestBody TokenRequest request) {
    Claims claims = new JwtUtil().parseJWT(request.getToken());
    Long userId = Long.valueOf(claims.getSubject());
    return userCollectService.pageCollect(pageNo, pageSize, userId);
  }

  @GetMapping("/one")
  @ApiOperation("根据Id查一条")
  public R<UserCollect> one(@RequestParam Long userCollectId) {
    return userCollectService.getOne(userCollectId);
  }

  @PostMapping("/doCollect")
  @ApiOperation("收藏or取消收藏")
  public R<String> doCollect(
      @RequestBody UserCollectVo userCollectVo) {
    userCollectVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    return userCollectService.doCollect(userCollectVo);
  }

  @PostMapping("/collect")
  @ApiOperation("收藏")
  public R<String> add(
      @RequestBody UserCollectVo userCollectVo) {
    userCollectVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    return userCollectService.addCollect(userCollectVo);
  }

  @PostMapping("/removeCollect")
  @ApiOperation("取消收藏")
  public R<String> delete(
      @RequestBody UserCollectVo userCollectVo) {
    userCollectVo.setUserId(Long.valueOf(new JwtUtil().getUserId()));
    return userCollectService.removeCollect(userCollectVo);
  }
}
